<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 角色权限
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="角色名称" class="handle-input mr10"></el-input>
                <el-input v-model="query.name" placeholder="角色权限" class="handle-input mr10"></el-input>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="序号" align="center" width="80"></el-table-column>
                <el-table-column prop="name" label="角色" width="280"></el-table-column>
                <el-table-column prop="name" label="权限"></el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            size="mini"
                            @click="editAuthority(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="danger"
                            size="mini"
                            @click="Delete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- table分页 -->
            <div class="pagination">
                <div class="btns">
                    <el-button type="danger" class="handle-del mr10" @click="allDelete">批量删除</el-button>
                </div>

                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="currentPage2"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="10"
                    layout="sizes, prev, pager, next"
                    :total="pageTotal"
                ></el-pagination>
            </div>

            <!-- 添加权限 -->
            <el-dialog title="添加权限" :visible.sync="addVisible" width="40%">
                <el-form
                    ref="AuthorityForm"
                    :model="AuthorityForm"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-form-item label="角色名称" prop="name">
                        <el-input v-model="AuthorityForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述">
                        <el-input type="textarea" v-model="AuthorityForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="拥有权限" prop="Authority">
                        <div style="margin-top: 20px">
                            <el-checkbox-group v-model="AuthorityForm.checkboxGroup1" size="small">
                                <el-checkbox label="系统管理" border></el-checkbox>
                                <el-checkbox label="商家申请" border></el-checkbox>
                                <el-checkbox label="返现记录" border></el-checkbox>
                                <el-checkbox label="商品管理" border></el-checkbox>
                                <el-checkbox label="订单管理" border></el-checkbox>
                                <el-checkbox label="提现记录" border></el-checkbox>
                                <el-checkbox label="社交管理" border></el-checkbox>
                                <el-checkbox label="服务设置" border></el-checkbox>
                                <el-checkbox label="小程序用户管理" border></el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveAdd">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 编辑权限 -->
            <el-dialog title="编辑权限" :visible.sync="editVisible" width="40%">
                <el-form
                    ref="AuthorityForm"
                    :model="AuthorityForm"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-form-item label="角色名称" prop="name">
                        <el-input v-model="AuthorityForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述">
                        <el-input type="textarea" v-model="AuthorityForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="拥有权限" prop="Authority">
                        <div style="margin-top: 20px">
                            <el-checkbox-group v-model="AuthorityForm.checkboxGroup1" size="small">
                                <el-checkbox label="系统管理" border></el-checkbox>
                                <el-checkbox label="商家申请" border></el-checkbox>
                                <el-checkbox label="返现记录" border></el-checkbox>
                                <el-checkbox label="商品管理" border></el-checkbox>
                                <el-checkbox label="订单管理" border></el-checkbox>
                                <el-checkbox label="提现记录" border></el-checkbox>
                                <el-checkbox label="社交管理" border></el-checkbox>
                                <el-checkbox label="服务设置" border></el-checkbox>
                                <el-checkbox label="小程序用户管理" border></el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
  name: 'RolePermissions',
  data() {
    return {
      query: {
        address: '',
        name: '',
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      addVisible: false,//添加权限
      editVisible: false,//编辑权限
      pageTotal: 0,
      form: {},
      AuthorityForm: {
        checkboxGroup1: ['系统管理']
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        Authority: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      idx: -1,
      id: -1
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      fetchData(this.query).then(res => {
        console.log(res);
        this.tableData = res.list;
        this.pageTotal = res.pageTotal || 50;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1);
      this.getData();
    },

    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 删除操作
    Delete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.$message.success('删除成功');
          this.tableData.splice(index, 1);
        })
        .catch(() => { });
    },
    // 批量删除
    allDelete() {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },

    //编辑权限
    editAuthority() {
      this.editVisible = true
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false;
      this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      this.$set(this.tableData, this.idx, this.form);
    },
    //添加操作
    handleAdd(index, row) {
      this.addVisible = true;
    },
    // 保存添加
    saveAdd() {
      this.addVisible = false;
    },




    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val);
      this.getData();
    },



  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    display: flex;
    align-items: center;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}
.handle-date {
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.pagination {
    display: flex;
    justify-content: space-between;
}
.el-checkbox.is-bordered + .el-checkbox.is-bordered {
    margin-left: 0;
}
</style>
